home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / var / lib / python-support / python2.6 / glchess / chess / lan.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.0 KB  |  206 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''
  5. '''
  6. __author__ = 'Robert Ancell <bob27@users.sourceforge.net>'
  7. __license__ = 'GNU General Public License Version 2'
  8. __copyright__ = 'Copyright 2005-2006  Robert Ancell'
  9. import board
  10. CHECK = '+'
  11. CHECKMATE = '#'
  12. MOVE = '-'
  13. TAKE = 'x'
  14. CASTLE_SHORT = 'o-o'
  15. CASTLE_LONG = 'o-o-o'
  16. _typeToLAN = {
  17.     board.PAWN: 'P',
  18.     board.KNIGHT: 'N',
  19.     board.BISHOP: 'B',
  20.     board.ROOK: 'R',
  21.     board.QUEEN: 'Q',
  22.     board.KING: 'K' }
  23. _lanToType = { }
  24. for pieceType, character in _typeToLAN.iteritems():
  25.     _lanToType[character] = pieceType
  26.     _lanToType[character.lower()] = pieceType
  27.  
  28.  
  29. class DecodeError(Exception):
  30.     '''
  31.     '''
  32.     pass
  33.  
  34.  
  35. def _checkLocation(location):
  36.     '''
  37.     '''
  38.     if len(location) != 2:
  39.         raise DecodeError('Invalid length location')
  40.     len(location) != 2
  41.     if location[0] < 'a' or location[0] > 'h':
  42.         raise DecodeError('Invalid rank')
  43.     location[0] > 'h'
  44.     if location[1] < '0' or location[1] > '8':
  45.         raise DecodeError('Invalid file')
  46.     location[1] > '8'
  47.     return location
  48.  
  49.  
  50. def decode(colour, move):
  51.     """Decode a long algebraic format move.
  52.     
  53.     'colour' is the colour of the player making the move (board.WHITE or board.BLACK).
  54.     'move' is the move description (string).
  55.  
  56.     Returns a tuple containing (start, end, piece, moveType, promotionType, result)
  57.     'start' is the location being moved from (string, e.g. 'a1', 'h8').
  58.     'end' is the location being moved to (string, e.g. 'a1', 'h8').
  59.     'piece' is the piece being moved (board.PAWN, board.ROOK, ... or None if not specified).
  60.     'moveType' is a flag to show if this move takes an oppoenent piece (MOVE, TAKE or None if not specified).
  61.     'promotionType' is the piece type to promote to (board.ROOK, board.KNIGHT, ... or None if not specified).
  62.     'check' is the result after the move (CHECK, CHECKMATE or None if not specified).
  63.     
  64.     Raises DecodeError if the move is unable to be decoded.
  65.     """
  66.     pieceType = None
  67.     promotionType = None
  68.     moveType = None
  69.     result = None
  70.     m = move
  71.     if colour is board.WHITE:
  72.         baseFile = '1'
  73.     else:
  74.         baseFile = '8'
  75.     if m == CASTLE_SHORT:
  76.         return ('e' + baseFile, 'g' + baseFile, None, None, None, None)
  77.     if m == CASTLE_LONG:
  78.         return ('e' + baseFile, 'c' + baseFile, None, None, None, None)
  79.     if len(m) < 1:
  80.         raise DecodeError('Too short')
  81.     len(m) < 1
  82.     
  83.     try:
  84.         pieceType = _lanToType[m[0]]
  85.     except KeyError:
  86.         m == CASTLE_LONG
  87.         m == CASTLE_LONG
  88.         m == CASTLE_SHORT
  89.         pieceType = None
  90.     except:
  91.         m == CASTLE_LONG
  92.  
  93.     m = m[1:]
  94.     if len(m) < 2:
  95.         raise DecodeError('Too short')
  96.     len(m) < 2
  97.     
  98.     try:
  99.         start = _checkLocation(m[:2])
  100.     except DecodeError:
  101.         m == CASTLE_LONG
  102.         e = m == CASTLE_LONG
  103.         m == CASTLE_SHORT
  104.         if pieceType is None:
  105.             raise e
  106.         pieceType is None
  107.         m = move
  108.         start = _checkLocation(m[:2])
  109.         pieceType = None
  110.     except:
  111.         m == CASTLE_LONG
  112.  
  113.     m = m[2:]
  114.     if len(m) < 1:
  115.         raise DecodeError('Too short')
  116.     len(m) < 1
  117.     if len(m) < 2:
  118.         raise DecodeError('Too short')
  119.     len(m) < 2
  120.     end = _checkLocation(m[:2])
  121.     m = m[2:]
  122.     if len(m) > 0:
  123.         if m[0] == '=':
  124.             if len(m) < 2:
  125.                 raise DecodeError('Too short')
  126.             len(m) < 2
  127.             
  128.             try:
  129.                 promotionType = _lanToType[m[1]]
  130.             except KeyError:
  131.                 None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
  132.                 None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
  133.                 raise DecodeError('Unknown promotion type')
  134.             except:
  135.                 None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
  136.  
  137.             m = m[2:]
  138.         else:
  139.             
  140.             try:
  141.                 promotionType = _lanToType[m[0]]
  142.             except KeyError:
  143.                 None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
  144.                 None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
  145.             except:
  146.                 None if m[0] == MOVE or m[0] == TAKE else m == CASTLE_SHORT
  147.  
  148.             m = m[1:]
  149.     
  150.     if len(m) == 1:
  151.         if m == CHECK or m == CHECKMATE:
  152.             result = m
  153.             m = ''
  154.         
  155.     elif len(m) == 2:
  156.         if m == '++':
  157.             result = CHECKMATE
  158.             m = ''
  159.         
  160.     
  161.     if len(m) != 0:
  162.         raise DecodeError('Extra characters')
  163.     len(m) != 0
  164.     return (start, end, pieceType, moveType, promotionType, result)
  165.  
  166.  
  167. def encode(colour, start, end, piece = None, moveType = None, promotionType = None, result = None):
  168.     """Encode a long algebraic format move.
  169.         
  170.     'start' is the location being moved from (string, e.g. 'a1', 'h8').
  171.     'end' is the location being moved to (string, e.g. 'a1', 'h8').
  172.     'piece' is the piece being moved (board.PAWN, board.ROOK, ... or None if not specified).
  173.     'moveType' is a flag to show if this move takes an oppoenent piece (MOVE, TAKE or None if not specified).
  174.     'promotionType' is the piece type to promote to (board.ROOK, board.KNIGHT, ... or None if not specified).
  175.     'check' is the result after the move (CHECK, CHECKMATE or None if not specified).
  176.         
  177.     Returns a string describing this move.
  178.     """
  179.     
  180.     try:
  181.         _checkLocation(start)
  182.         _checkLocation(end)
  183.     except DecodeError:
  184.         raise TypeError("Invalid values for 'start' and 'end'")
  185.  
  186.     string = ''
  187.     if piece is not None:
  188.         string += _typeToLAN[piece]
  189.     
  190.     string += start
  191.     if moveType != None:
  192.         string += moveType
  193.     
  194.     string += end
  195.     if promotionType != None:
  196.         if False:
  197.             string += '='
  198.         
  199.         string += _typeToLAN[promotionType].lower()
  200.     
  201.     if result is not None:
  202.         string += result
  203.     
  204.     return string
  205.  
  206.